﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.SIESystemManager.Calculate
{
    public class C_D27
    {
        public double GetD27(DataRow[] dr1, DataRow[] dr2)
        {
            double DBHours = 0;//打包工时
            double TD27 = 0, TD27a = 0, TD27b = 0, TD27c = 0, TD27d = 0, Tw1 = 0, Tw2 = 0, Td1 = 0, Td2 = 0, TWorkStation1 = 0, TWorkStation2 = 0, TWorkStation3 = 0;
            double TGLUE_AND_PADDING = 0;//喷胶及扪皮
            double TLOCK_BLOCK_SCREW = 0;//锁挡片螺丝
            double TASSEMBLE_L_TYPE_TAPE = 0;//装L型胶条
            double TASSEMBLE_DOOR_PARTS = 0;//装门舌配件
            double TASSEMBLE_DOOR_TAPE = 0;//装趟门胶条
            double THOLE_COPPER_PLUG = 0;//打铜头胶塞
            double TTWIST_TOOTHED_NUT = 0;//拧带齿螺母
            double TGROOVE_COLOR_SIDE = 0;//槽侧边擦色
            double TPASTE_COMMON_LABLE = 0;//贴通用标签
            double TPASTE_WHITE_TAPE = 0;//贴白色胶纸
            double TPASTE_LAMEX_LOGO = 0;//贴美时LOGO
            double TLOCK_SELF_TAPPING_SCREW = 0;//锁自攻螺丝
            double TPASTE_DOUBLE_TAPE = 0;//贴双面胶纸
            double TASSEMBLE_SCREEN_CODE = 0;//装屏风挂码
            double TASSEMBLE_KONG_GAI = 0;//装装饰孔盖

            double WGLUE_AND_PADDING = 0;//单位工时-喷胶及扪皮
            double AGLUE_AND_PADDING = 0;//附加工时-喷胶及扪皮
            double WLOCK_BLOCK_SCREW = 0;//单位工时-锁挡片螺丝
            double ALOCK_BLOCK_SCREW = 0;//附加工时-锁挡片螺丝
            double WASSEMBLE_L_TYPE_TAPE = 0;//单位工时-装L型胶条
            double AASSEMBLE_L_TYPE_TAPE = 0;//附加工时-装L型胶条
            double WASSEMBLE_DOOR_PARTS = 0;//单位工时-装门舌配件
            double AASSEMBLE_DOOR_PARTS = 0;//附加工时-装门舌配件
            double WASSEMBLE_DOOR_TAPE = 0;//单位工时-装趟门胶条
            double AASSEMBLE_DOOR_TAPE = 0;//附加工时-装趟门胶条
            double WGROOVE_COLOR_SIDE = 0;//单位工时-槽侧边擦色
            double AGROOVE_COLOR_SIDE = 0;//附件工时-槽侧边擦色
            double WHOLE_COPPER_PLUG = 0;//单位工时-打铜头胶塞
            double AHOLE_COPPER_PLUG = 0;//附件工时-打铜头胶塞
            double WTWIST_TOOTHED_NUT = 0;//单位工时-拧带齿螺母
            double ATWIST_TOOTHED_NUT = 0;//附件工时-拧带齿螺母
            double WPASTE_COMMON_LABLE = 0;//单位工时-贴通用标签
            double APASTE_COMMON_LABLE = 0;//附件工时-贴通用标签
            double WPASTE_WHITE_TAPE = 0;//单位工时-贴白色胶纸
            double APASTE_WHITE_TAPE = 0;//附加工时-贴白色胶纸
            double WPASTE_LAMEX_LOGO = 0;//单位工时-贴美时LOGO
            double APASTE_LAMEX_LOGO = 0;//附件工时-贴美时LOGO
            double WLOCK_SELF_TAPPING_SCREW = 0;//单位工时-锁自攻螺丝
            double ALOCK_SELF_TAPPING_SCREW = 0;//附加工时-锁自攻螺丝
            double WPASTE_DOUBLE_TAPE = 0;//单位工时-贴双面胶纸
            double APASTE_DOUBLE_TAPE = 0;//附加工时-贴双面胶纸
            double WASSEMBLE_SCREEN_CODE = 0;//单位工时-装屏风挂码
            double AASSEMBLE_SCREEN_CODE = 0;//附加工时-装屏风挂码
            double WASSEMBLE_KONG_GAI = 0;//单位工时-装装饰孔盖
            double AASSEMBLE_KONG_GAI = 0;//附加工时-装装饰孔盖

            //获取数据库每个工序动作的单位工时和附加工时
            foreach (var drHours in dr2)
            {
                switch (drHours["OPERATIONAL_NAME"].ToString())
                {
                    case "喷胶及扪皮":
                        WGLUE_AND_PADDING = double.Parse(string.IsNullOrEmpty(drHours["UNIT_WORKHOURS"].ToString()) ? "0" : drHours["UNIT_WORKHOURS"].ToString());//单位工时
                        AGLUE_AND_PADDING = double.Parse(string.IsNullOrEmpty(drHours["ATTACH_WORKHOURS"].ToString()) ? "0" : drHours["ATTACH_WORKHOURS"].ToString());//附加工时
                        break;
                    case "锁挡片螺丝":
                        WLOCK_BLOCK_SCREW = double.Parse(string.IsNullOrEmpty(drHours["UNIT_WORKHOURS"].ToString()) ? "0" : drHours["UNIT_WORKHOURS"].ToString());//单位工时
                        ALOCK_BLOCK_SCREW = double.Parse(string.IsNullOrEmpty(drHours["ATTACH_WORKHOURS"].ToString()) ? "0" : drHours["ATTACH_WORKHOURS"].ToString());//附加工时
                        break;
                    case "装L型胶条":
                        WASSEMBLE_L_TYPE_TAPE = double.Parse(string.IsNullOrEmpty(drHours["UNIT_WORKHOURS"].ToString()) ? "0" : drHours["UNIT_WORKHOURS"].ToString());//单位工时
                        AASSEMBLE_L_TYPE_TAPE = double.Parse(string.IsNullOrEmpty(drHours["ATTACH_WORKHOURS"].ToString()) ? "0" : drHours["ATTACH_WORKHOURS"].ToString());//附加工时
                        break;
                    case "装门舌配件":
                        WASSEMBLE_DOOR_PARTS = double.Parse(string.IsNullOrEmpty(drHours["UNIT_WORKHOURS"].ToString()) ? "0" : drHours["UNIT_WORKHOURS"].ToString());//单位工时
                        AASSEMBLE_DOOR_PARTS = double.Parse(string.IsNullOrEmpty(drHours["ATTACH_WORKHOURS"].ToString()) ? "0" : drHours["ATTACH_WORKHOURS"].ToString());//附加工时
                        break;
                    case "装趟门胶条":
                        WASSEMBLE_DOOR_TAPE = double.Parse(string.IsNullOrEmpty(drHours["UNIT_WORKHOURS"].ToString()) ? "0" : drHours["UNIT_WORKHOURS"].ToString());//单位工时
                        AASSEMBLE_DOOR_TAPE = double.Parse(string.IsNullOrEmpty(drHours["ATTACH_WORKHOURS"].ToString()) ? "0" : drHours["ATTACH_WORKHOURS"].ToString());//附加工时
                        break;
                    case "槽侧边擦色":
                        WGROOVE_COLOR_SIDE = double.Parse(string.IsNullOrEmpty(drHours["UNIT_WORKHOURS"].ToString()) ? "0" : drHours["UNIT_WORKHOURS"].ToString());//单位工时
                        AGROOVE_COLOR_SIDE = double.Parse(string.IsNullOrEmpty(drHours["ATTACH_WORKHOURS"].ToString()) ? "0" : drHours["ATTACH_WORKHOURS"].ToString());//附加工时
                        break;
                    case "打铜头胶塞":
                        WHOLE_COPPER_PLUG = double.Parse(string.IsNullOrEmpty(drHours["UNIT_WORKHOURS"].ToString()) ? "0" : drHours["UNIT_WORKHOURS"].ToString());//单位工时
                        AHOLE_COPPER_PLUG = double.Parse(string.IsNullOrEmpty(drHours["ATTACH_WORKHOURS"].ToString()) ? "0" : drHours["ATTACH_WORKHOURS"].ToString());//附加工时
                        break;
                    case "拧带齿螺母":
                        WTWIST_TOOTHED_NUT = double.Parse(string.IsNullOrEmpty(drHours["UNIT_WORKHOURS"].ToString()) ? "0" : drHours["UNIT_WORKHOURS"].ToString());//单位工时
                        ATWIST_TOOTHED_NUT = double.Parse(string.IsNullOrEmpty(drHours["ATTACH_WORKHOURS"].ToString()) ? "0" : drHours["ATTACH_WORKHOURS"].ToString());//附加工时
                        break;
                    case "贴美时LOGO":
                        WPASTE_LAMEX_LOGO = double.Parse(string.IsNullOrEmpty(drHours["UNIT_WORKHOURS"].ToString()) ? "0" : drHours["UNIT_WORKHOURS"].ToString());//单位工时
                        APASTE_LAMEX_LOGO = double.Parse(string.IsNullOrEmpty(drHours["ATTACH_WORKHOURS"].ToString()) ? "0" : drHours["ATTACH_WORKHOURS"].ToString());//附加工时
                        break;
                    case "贴通用标签":
                        WPASTE_COMMON_LABLE = double.Parse(string.IsNullOrEmpty(drHours["UNIT_WORKHOURS"].ToString()) ? "0" : drHours["UNIT_WORKHOURS"].ToString());//单位工时
                        APASTE_COMMON_LABLE = double.Parse(string.IsNullOrEmpty(drHours["ATTACH_WORKHOURS"].ToString()) ? "0" : drHours["ATTACH_WORKHOURS"].ToString());//附加工时
                        break;
                    case "贴白色胶纸":
                        WPASTE_WHITE_TAPE = double.Parse(string.IsNullOrEmpty(drHours["UNIT_WORKHOURS"].ToString()) ? "0" : drHours["UNIT_WORKHOURS"].ToString());//单位工时
                        APASTE_WHITE_TAPE = double.Parse(string.IsNullOrEmpty(drHours["ATTACH_WORKHOURS"].ToString()) ? "0" : drHours["ATTACH_WORKHOURS"].ToString());//附加工时
                        break;
                    case "锁自攻螺丝":
                        WLOCK_SELF_TAPPING_SCREW = double.Parse(string.IsNullOrEmpty(drHours["UNIT_WORKHOURS"].ToString()) ? "0" : drHours["UNIT_WORKHOURS"].ToString());//单位工时
                        ALOCK_SELF_TAPPING_SCREW = double.Parse(string.IsNullOrEmpty(drHours["ATTACH_WORKHOURS"].ToString()) ? "0" : drHours["ATTACH_WORKHOURS"].ToString());//附加工时
                        break;
                    case "贴双面胶纸":
                        WPASTE_DOUBLE_TAPE = double.Parse(string.IsNullOrEmpty(drHours["UNIT_WORKHOURS"].ToString()) ? "0" : drHours["UNIT_WORKHOURS"].ToString());//单位工时
                        APASTE_DOUBLE_TAPE = double.Parse(string.IsNullOrEmpty(drHours["ATTACH_WORKHOURS"].ToString()) ? "0" : drHours["ATTACH_WORKHOURS"].ToString());//附加工时
                        break;
                    case "装屏风挂码":
                        WASSEMBLE_SCREEN_CODE = double.Parse(string.IsNullOrEmpty(drHours["UNIT_WORKHOURS"].ToString()) ? "0" : drHours["UNIT_WORKHOURS"].ToString());//单位工时
                        AASSEMBLE_SCREEN_CODE = double.Parse(string.IsNullOrEmpty(drHours["ATTACH_WORKHOURS"].ToString()) ? "0" : drHours["ATTACH_WORKHOURS"].ToString());//附加工时
                        break;
                    case "装装饰孔盖":
                        WASSEMBLE_KONG_GAI = double.Parse(string.IsNullOrEmpty(drHours["UNIT_WORKHOURS"].ToString()) ? "0" : drHours["UNIT_WORKHOURS"].ToString());//单位工时
                        AASSEMBLE_KONG_GAI = double.Parse(string.IsNullOrEmpty(drHours["ATTACH_WORKHOURS"].ToString()) ? "0" : drHours["ATTACH_WORKHOURS"].ToString());//附加工时
                        break;
                }
            }
            foreach (var drData in dr1)
            {
                Tw1 = double.Parse(string.IsNullOrEmpty(drData["Width_1"].ToString().Replace("*", "")) ? "0" : drData["Width_1"].ToString().Replace("*", ""));//W1
                Tw2 = double.Parse(string.IsNullOrEmpty(drData["Width_2"].ToString().Replace("*", "")) ? "0" : drData["Width_2"].ToString().Replace("*", ""));//w2
                Td1 = double.Parse(string.IsNullOrEmpty(drData["Depth_1"].ToString().Replace("*", "")) ? "0" : drData["Depth_1"].ToString().Replace("*", ""));//d1
                Td2 = double.Parse(string.IsNullOrEmpty(drData["Depth_2"].ToString().Replace("*", "")) ? "0" : drData["Depth_2"].ToString().Replace("*", ""));//d2

                TGLUE_AND_PADDING = double.Parse(string.IsNullOrEmpty(drData["D27_Spray_Glue_And_Tincture"].ToString()) ? "0" : drData["D27_Spray_Glue_And_Tincture"].ToString());//喷胶及扪皮
                TLOCK_BLOCK_SCREW = double.Parse(string.IsNullOrEmpty(drData["D27_Lock_Block_Screw"].ToString()) ? "0" : drData["D27_Lock_Block_Screw"].ToString());//锁挡片螺丝
                TASSEMBLE_L_TYPE_TAPE = double.Parse(string.IsNullOrEmpty(drData["D27_Loaded_L_Type_Tape"].ToString()) ? "0" : drData["D27_Loaded_L_Type_Tape"].ToString());//装L型胶条
                TASSEMBLE_DOOR_PARTS = double.Parse(string.IsNullOrEmpty(drData["D27_Fittings_For_Door_Tongue"].ToString()) ? "0" : drData["D27_Fittings_For_Door_Tongue"].ToString());//装门舌配件
                TASSEMBLE_DOOR_TAPE = double.Parse(string.IsNullOrEmpty(drData["D27_Door_Tape"].ToString()) ? "0" : drData["D27_Door_Tape"].ToString());//装趟门胶条        
                TGROOVE_COLOR_SIDE = double.Parse(string.IsNullOrEmpty(drData["D27_Groove_Side_Rubbing"].ToString()) ? "0" : drData["D27_Groove_Side_Rubbing"].ToString());//槽侧边擦色
                THOLE_COPPER_PLUG = double.Parse(string.IsNullOrEmpty(drData["D27_Copper_Head_Rubber_Plug"].ToString()) ? "0" : drData["D27_Copper_Head_Rubber_Plug"].ToString());//打铜头胶塞
                TTWIST_TOOTHED_NUT = double.Parse(string.IsNullOrEmpty(drData["D27_Twisted_Tooth_Nut"].ToString()) ? "0" : drData["D27_Twisted_Tooth_Nut"].ToString());//拧带齿螺母
                TPASTE_LAMEX_LOGO = double.Parse(string.IsNullOrEmpty(drData["D27_LOGO"].ToString()) ? "0" : drData["D27_LOGO"].ToString());//贴美时LOGO
                TPASTE_COMMON_LABLE = double.Parse(string.IsNullOrEmpty(drData["D27_General_Label"].ToString()) ? "0" : drData["D27_General_Label"].ToString());//贴通用标签
                TPASTE_WHITE_TAPE = double.Parse(string.IsNullOrEmpty(drData["D27_Paste_White_Adhesive_Paper"].ToString()) ? "0" : drData["D27_Paste_White_Adhesive_Paper"].ToString());//贴白色胶纸
                TLOCK_SELF_TAPPING_SCREW = double.Parse(string.IsNullOrEmpty(drData["D27_Lock_Self_Tapping_Screw"].ToString()) ? "0" : drData["D27_Lock_Self_Tapping_Screw"].ToString());//锁自攻螺丝
                TPASTE_DOUBLE_TAPE = double.Parse(string.IsNullOrEmpty(drData["D27_Double_Sided_Adhesive_Paper"].ToString()) ? "0" : drData["D27_Double_Sided_Adhesive_Paper"].ToString());//贴双面胶纸
                TASSEMBLE_SCREEN_CODE = double.Parse(string.IsNullOrEmpty(drData["D27_Mounting_Screen_Hanging_Code"].ToString()) ? "0" : drData["D27_Mounting_Screen_Hanging_Code"].ToString());//装屏风挂码
                TASSEMBLE_KONG_GAI = double.Parse(string.IsNullOrEmpty(drData["D27_Decorative_Hole_Cover"].ToString()) ? "0" : drData["D27_Decorative_Hole_Cover"].ToString());//装装饰孔盖
            }
            //计算T27b工时
            if (THOLE_COPPER_PLUG != 0)
            {
                TD27b = (THOLE_COPPER_PLUG * WHOLE_COPPER_PLUG) + AHOLE_COPPER_PLUG;//打铜头胶塞
            }
            if (TTWIST_TOOTHED_NUT != 0)
            {
                TD27b = TD27b + (TTWIST_TOOTHED_NUT * WTWIST_TOOTHED_NUT) + ATWIST_TOOTHED_NUT;//拧带齿螺母
            }

            //计算工位1工时
            if (TGLUE_AND_PADDING != 0 || TLOCK_BLOCK_SCREW != 0 || TASSEMBLE_L_TYPE_TAPE != 0 || TASSEMBLE_DOOR_PARTS != 0 || TASSEMBLE_DOOR_TAPE != 0)
            {
                TD27a = (2 * (Tw1 + Tw2) * 0.37744 * TGLUE_AND_PADDING) + AGLUE_AND_PADDING;//喷胶及扪皮
                TD27a = TD27a + (WLOCK_BLOCK_SCREW * TLOCK_BLOCK_SCREW) + ALOCK_BLOCK_SCREW;//锁挡片螺丝
                if (TASSEMBLE_L_TYPE_TAPE != 0)
                {
                    if (NumMAX(Tw1, Tw2) < 1000)//装L型胶条
                    {
                        TD27a = TD27a + 50 + (15 * TASSEMBLE_L_TYPE_TAPE);
                    }
                    else
                    {
                        TD27a = TD27a + 55 + (20 * TASSEMBLE_L_TYPE_TAPE);
                    }
                }
                TD27a = TD27a + (WASSEMBLE_DOOR_PARTS * TASSEMBLE_DOOR_PARTS) + AASSEMBLE_DOOR_PARTS;//装门舌配件
                if (TASSEMBLE_DOOR_TAPE != 0)
                {
                    if (NumMAX(Tw1, Tw2) < 1000)//装趟门胶条
                    {
                        TD27a = TD27a + 50 + (15 * TASSEMBLE_DOOR_TAPE);
                    }
                    else
                    {
                        TD27a = TD27a + 55 + (20 * TASSEMBLE_DOOR_TAPE);
                    }
                }
                TWorkStation1 = TD27a + TD27b;//工位1工时
            }

            //计算工位2工时
            if (TGROOVE_COLOR_SIDE != 0)
            {
                TWorkStation2 = (TGROOVE_COLOR_SIDE * WGROOVE_COLOR_SIDE) + AGROOVE_COLOR_SIDE;//槽侧边擦色
            }
            if (TPASTE_COMMON_LABLE != 0)
            {
                TWorkStation2 = TWorkStation2 + (TPASTE_COMMON_LABLE * WPASTE_COMMON_LABLE) + APASTE_COMMON_LABLE;//贴通用标签
            }
            //清洗时间
            if (TGLUE_AND_PADDING != 0 || TLOCK_BLOCK_SCREW != 0 || TASSEMBLE_L_TYPE_TAPE != 0 || TASSEMBLE_DOOR_PARTS != 0 ||
                TASSEMBLE_DOOR_TAPE != 0 || TGROOVE_COLOR_SIDE != 0 || THOLE_COPPER_PLUG != 0 || TTWIST_TOOTHED_NUT != 0 ||
                TPASTE_LAMEX_LOGO != 0 || TPASTE_COMMON_LABLE != 0 || TPASTE_WHITE_TAPE != 0 || TLOCK_SELF_TAPPING_SCREW != 0 ||
                TPASTE_DOUBLE_TAPE != 0 || TASSEMBLE_SCREEN_CODE != 0 || TASSEMBLE_KONG_GAI != 0)
            {
                //if (NumMAX(Tw1, Tw2) < 1000)
                //{
                //    TD27c = (0.000079 * Tw1 * Tw2 * 2) + 8;
                //}
                //else
                //{
                TD27c = (0.000058 * Tw1 * Tw2 * 2) + 10;
                // }
                DBHours = 30;//打包工时赋值
            }

            TWorkStation2 = TWorkStation2 + TD27c;//工位2工时

            //计算TD27d工时
            if (TPASTE_WHITE_TAPE != 0)
            {
                TD27d = (TPASTE_WHITE_TAPE * WPASTE_WHITE_TAPE) + APASTE_WHITE_TAPE;//贴白色胶纸               
            }
            if (TPASTE_LAMEX_LOGO != 0)
            {
                TD27d = TD27d + (TPASTE_LAMEX_LOGO * WPASTE_LAMEX_LOGO) + APASTE_LAMEX_LOGO;//贴美时LOGO
            }
            if (TLOCK_SELF_TAPPING_SCREW != 0)
            {
                TD27d = TD27d + (TLOCK_SELF_TAPPING_SCREW * WLOCK_SELF_TAPPING_SCREW) + ALOCK_SELF_TAPPING_SCREW;//锁自攻螺丝
            }
            if (TPASTE_DOUBLE_TAPE != 0)
            {
                TD27d = TD27d + (TPASTE_DOUBLE_TAPE * WPASTE_DOUBLE_TAPE) + APASTE_DOUBLE_TAPE;//贴双面胶纸
            }
            if (TASSEMBLE_SCREEN_CODE != 0)
            {
                TD27d = TD27d + (TASSEMBLE_SCREEN_CODE * WASSEMBLE_SCREEN_CODE) + AASSEMBLE_SCREEN_CODE;//装屏风挂码
            }
            if (TASSEMBLE_KONG_GAI != 0)
            {
                TD27d = TD27d + (TASSEMBLE_KONG_GAI * WASSEMBLE_KONG_GAI) + AASSEMBLE_KONG_GAI;//装装饰孔盖
            }
            //计算工位3工时
            if (TD27a == 0)
            {
                TWorkStation3 = TD27d + DBHours + TD27b;
            }
            else
            {
                TWorkStation3 = TD27d + DBHours;
            }
            //Total工时
            TD27 = TWorkStation1 + (NumMAX(TWorkStation2, TWorkStation3) * 2);
            return TD27;
        }
        private static double NumMAX(double Tw1, double Tw2)
        {
            if (Tw1 > Tw2)
            {
                return Tw1;
            }
            return Tw2;
        }
    }
}
